Data Sources:

https://www.kaggle.com/new-york-state/nys-children-in-foster-care-annually https://www.ncsc.org/Microsites/EveryKid/Home/Data-and-Reform-Efforts/Data-By-State.aspx https://www.acf.hhs.gov/cb/resource/trends-in-foster-care-and-adoption

library(readxl)
library(tidyverse)
library(viridis)
library(plotly)
library(sf)
library(leaflet)

Read the data

#national dataset
nation_data<-read_excel("data/national_afcars_trends_2009_through_2018.xlsx",sheet="Data")

#State dataset
#Numbers of Children Served in Foster Care, by State
state_served <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Served!A8:K60") %>%
gather(year,Served,'FY 2009':'FY 2018')

#Numbers of Children in Foster Care on September 30th, by State
state_inCare <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="In Care on September 30th!A8:K60") %>%
gather(year,InCare_Sep30,'FY 2009':'FY 2018')

#Numbers of Children Entering Foster Care, by State
state_entered <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Entered!A8:K60") %>%
gather(year,Entered,'FY 2009':'FY 2018')

#Numbers of Children Exiting Foster Care, by State
state_exited <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Exited!A8:K60") %>%
gather(year,Exited,'FY 2009':'FY 2018')

#Numbers of Children Waiting for Adoption, by State
state_waitingAdoption <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Waiting for Adoption!A8:K60") %>%
gather(year,Waiting_Adoption,'FY 2009':'FY 2018')

#Numbers of Children Waiting for Adoption Whose Parental Rights Have Been Terminated, by State
state_parentalRightsTerminated <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Parental Rights Terminated!A8:K60") %>%
gather(year,parental_rights_terminated,'FY 2009':'FY 2018')

#Numbers of Children Adopted, by State
state_adopted <- read_excel("data/afcars_state_data_tables_09thru18.xlsx",range="Adopted!A8:K60") %>%
gather(year,adopted,'FY 2009':'FY 2018')

Merge the data for all categories for states

merge_cols<-c("State","year")
#The merge argument only takes two values as input, so you have to do them separately:
#state_df<- merge(state_served,state_inCare,state_entered,state_exited,state_waitingAdoption,state_parentalRightsTerminated,state_adopted,by=c("State","year"))

state_data<- merge(state_served,state_inCare,by=merge_cols)
state_data<- merge(state_data,state_entered,by=merge_cols)
state_data<- merge(state_data,state_exited,by=merge_cols)
state_data<- merge(state_data,state_waitingAdoption,by=merge_cols)
state_data<- merge(state_data,state_parentalRightsTerminated,by=merge_cols)
state_data<- merge(state_data,state_adopted,by=merge_cols)

CHeck the data

head(state_data)
##     State    year Served InCare_Sep30 Entered Exited Waiting_Adoption
## 1 Alabama FY 2009   9677         6179    3080   3498             1475
## 2 Alabama FY 2010   8119         5350    3063   2770             1271
## 3 Alabama FY 2011   8395         5253    3257   3143             1297
## 4 Alabama FY 2012   7907         4561    2763   3346             1156
## 5 Alabama FY 2013   7322         4435    3041   2888             1084
## 6 Alabama FY 2014   7520         4526    3192   2994             1044
##   parental_rights_terminated adopted
## 1                        882     638
## 2                        757     606
## 3                        701     447
## 4                        543     587
## 5                        615     532
## 6                        573     544

Read the shape file

http://strimas.com/r/tidy-sf/

us_states <- st_read("./shp/states.shp")
## Reading layer `states' from data source `/Volumes/GoogleDrive/My Drive/RWorkspace/VisualAnalytics-FinalProject/shp/states.shp' using driver `ESRI Shapefile'
## Simple feature collection with 51 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -178.2176 ymin: 18.92179 xmax: -66.96927 ymax: 71.40624
## epsg (SRID):    4269
## proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs

Preview as a tibble

as_tibble(us_states)
## # A tibble: 51 x 6
##    STATE_NAME DRAWSEQ STATE_FIPS SUB_REGION STATE_ABBR                  geometry
##    <fct>        <int> <fct>      <fct>      <fct>             <MULTIPOLYGON [°]>
##  1 Hawaii           1 15         Pacific    HI         (((-160.0738 22.00418, -…
##  2 Washington       2 53         Pacific    WA         (((-122.402 48.22522, -1…
##  3 Montana          3 30         Mountain   MT         (((-111.4754 44.70216, -…
##  4 Maine            4 23         New Engla… ME         (((-69.77728 44.07415, -…
##  5 North Dak…       5 38         West Nort… ND         (((-98.73044 45.93827, -…
##  6 South Dak…       6 46         West Nort… SD         (((-102.7884 42.9953, -1…
##  7 Wyoming          7 56         Mountain   WY         (((-104.0536 41.69822, -…
##  8 Wisconsin        8 55         East Nort… WI         (((-87.74856 44.96162, -…
##  9 Idaho            9 16         Mountain   ID         (((-117.0263 43.67903, -…
## 10 Vermont         10 50         New Engla… VT         (((-73.25806 42.74606, -…
## # … with 41 more rows

Merge our data for for FY 2009

Filter the data for 2009 and rename the state column

state_data_2009 <- state_data %>% filter(year == 'FY 2009') %>% rename(STATE_NAME = State)

Merge the data with the shape file to get the State Codes

us_states_mapped <- inner_join(us_states,state_data_2009,by="STATE_NAME")
## Warning: Column `STATE_NAME` joining factor and character vector, coercing into
## character vector

Plot the data

https://plot.ly/r/choropleth-maps/

check the test dataframe

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
head(df)
##   code      state category total.exports  beef pork poultry  dairy fruits.fresh
## 1   AL    Alabama    state       1390.63  34.4 10.6   481.0   4.06          8.0
## 2   AK     Alaska    state         13.31   0.2  0.1     0.0   0.19          0.0
## 3   AZ    Arizona    state       1463.17  71.3 17.9     0.0 105.48         19.3
## 4   AR   Arkansas    state       3586.02  53.2 29.4   562.9   3.53          2.2
## 5   CA California    state      16472.88 228.7 11.1   225.4 929.95       2791.8
## 6   CO   Colorado    state       1851.33 261.4 66.0    14.0  71.94          5.7
##   fruits.proc total.fruits veggies.fresh veggies.proc total.veggies  corn wheat
## 1        17.1        25.11           5.5          8.9         14.33  34.9  70.0
## 2         0.0         0.00           0.6          1.0          1.56   0.0   0.0
## 3        41.0        60.27         147.5        239.4        386.91   7.3  48.7
## 4         4.7         6.88           4.4          7.1         11.45  69.5 114.5
## 5      5944.6      8736.40         803.2       1303.5       2106.79  34.6 249.3
## 6        12.2        17.99          45.1         73.2        118.27 183.2 400.5
##    cotton
## 1  317.61
## 2    0.00
## 3  423.95
## 4  665.44
## 5 1064.95
## 6    0.00

Plot the data

Plotly Viridis color pallete - https://www.r-bloggers.com/how-to-use-viridis-colors-with-plotly-and-leaflet/

#Set hover text
us_states_mapped$hover <- with(us_states_mapped,paste(STATE_NAME,'<br>',"Served: ", Served))
# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)
plot_geo(us_states_mapped, locationmode = 'USA-states') %>%
  add_trace(
    z = ~Served, text = ~hover, locations = ~STATE_ABBR,
    color = ~Served, colors = viridis_pal(option = "D")(3)
  ) %>%
  colorbar(title = "Served") %>%
  layout(
    title = 'Orphans Served by each state in 2009<br>(Hover for breakdown)',
    geo = g
  )

We provide viridis colors to plotly using viridis_pal and by setting option argument to “D” the default “viridis” is selected. Other options are “A” for “magma” theme, “B” – “inferno” and “C” – “plasma”. Play with letters to check which one you like the most or which suits your plot the best.

Trying in leaflet now

https://stackoverflow.com/questions/43434898/choropleth-maps-in-r-using-leaflet-package

popup1 <- paste0("<span style='color: #7f0000'><strong>US State Values</strong></span>",
                 "<br><span style='color: salmon;'><strong>State: </strong></span>", 
                 us_states_mapped$STATE_NAME, 
                 "<br><span style='color: salmon;'><strong>Served: </strong></span>", 
                 us_states_mapped$Served)
pal <- leaflet::colorFactor(viridis_pal(option = "D")(3), domain = us_states_mapped$Served)
leaflet(us_states_mapped) %>% 
  addProviderTiles("OpenStreetMap.Mapnik") %>%
  addPolygons(data = us_states_mapped, 
              fillColor = ~pal(Served),
              fillOpacity = 0.6,       
              color = "darkgrey",      
              weight = 1.5,            
              popup = popup1)
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs).
## Need '+proj=longlat +datum=WGS84'
  # addLegend(pal = pal, values = ~Served, opacity = 0.7, title = NULL,
  # position = "bottomright")